package com.timoon.manager.panel.product;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigation;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.wicket.jquery.ui.form.button.AjaxButton;
import com.googlecode.wicket.jquery.ui.panel.JQueryFeedbackPanel;
import com.googlecode.wicket.jquery.ui.widget.dialog.DialogButton;
import com.googlecode.wicket.jquery.ui.widget.dialog.DialogButtons;
import com.googlecode.wicket.jquery.ui.widget.dialog.DialogIcon;
import com.googlecode.wicket.jquery.ui.widget.dialog.MessageDialog;
import com.timoon.component.panel.bootstrap.modal.ModalPanel;
import com.timoon.component.panel.common.DefaultEmptyPanel;
import com.timoon.component.panel.jlayout.JLayouterPanel;
import com.timoon.manager.auth.BaseAuthPanel;
import com.timoon.sample.dao.model.Product;
import com.timoon.sample.dao.model.ProductSort;

public class ProductGridPanel extends BaseAuthPanel {
	
	@SpringBean(name="manager.product")
	private ProductGridService service;
	
	private ModalWindow win;
	
	private List<String> checkboxSelectedId=new ArrayList<String>();
	
	private JQueryFeedbackPanel feedbackPanel;
	
	private ModalPanel warnDialog;
	
	private WebMarkupContainer tableContainer;

	public ProductGridPanel(String id) {
		super(id);
		// TODO Auto-generated constructor stub	
		
		renderPanel();
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		renderWin();
		
		renderGrid();
		
		renderFeedbackPanel();
		
		renderDelWarningDialog();
		
		
	}

	private void renderDelWarningDialog() {
		// TODO Auto-generated method stub
//		warnDialog=new MessageDialog("wk_jquery_message_dialog","警告","确定删除所选项吗?",DialogButtons.YES_NO,DialogIcon.WARN) {
//			
//			@Override
//			protected void onClose(AjaxRequestTarget arg0, DialogButton arg1) {
//				// TODO Auto-generated method stub
//				
//				if(arg1.toString().equals("Yes")){
//					service.deleteProductByIds(checkboxSelectedId);
//					feedbackPanel.info("删除成功,请刷新");
//					
//					updateCenterPanel(arg0);
//				}else{
//					feedbackPanel.info("删除失败，用户取消");
//					arg0.add(feedbackPanel);
//				}
//				
//				
//			}
//		};
		
		warnDialog=new ModalPanel("wk_jquery_message_dialog","警告","确定","<div align='left' style='color:red;'>确定删除所选项吗?</div>") {
			@Override
			public boolean onAction(AjaxRequestTarget target) {
				// TODO Auto-generated method stub
				
				service.deleteProductByIds(checkboxSelectedId);
				return super.onAction(target);
			}
			@Override
			public void afterCloseWin(AjaxRequestTarget target) {
				// TODO Auto-generated method stub
				super.afterCloseWin(target);
				updateCenterPanel(target);
			}
		};
		add(warnDialog);
	}

	private void renderFeedbackPanel() {
		// TODO Auto-generated method stub
		this.feedbackPanel=new JQueryFeedbackPanel("wk_feedback");
		add(feedbackPanel);
	}

	private void renderWin() {
		// TODO Auto-generated method stub
		win=new ModalWindow("wk_win");
		win.setTitle("商品详细");
		add(win);
	}

	private void renderGrid() {
		// TODO Auto-generated method stub
		
		List<Product> products=this.service.getAllProduct();
		
		Form form=new Form<>("wk_form");
		add(form);
		
		renderBtns(form);
		
		renderTabelContainer(form);
		
		PageableListView<Product> listView=new PageableListView<Product>("wk_list",products,10) {
			
			@Override
			protected void populateItem(ListItem<Product> arg0) {
				// TODO Auto-generated method stub
				
				final Product product=arg0.getModelObject();
				
				arg0.add(new AjaxCheckBox("wk_checkbox",new Model<Boolean>(false)) {

					@Override
					protected void onUpdate(AjaxRequestTarget arg0) {
						// TODO Auto-generated method stub
						if((boolean) this.getDefaultModelObject()){
							checkboxSelectedId.add(product.getId().toString());
						}else{
							if(checkboxSelectedId.contains(product.getId().toString())){
								checkboxSelectedId.remove(product.getId().toString());
							}
						}
						
					}
					
				});
				
				arg0.add(new Label("wk_id",product.getId()));
				
				arg0.add(new Label("wk_name",product.getName()));
				
				arg0.add(new Label("wk_market_price",product.getMarketPrice()));
				
				arg0.add(new Label("wk_actual_price",product.getActualPrice()));
				
				ProductSort sort=service.getSortByProductId(product.getSortId());
				arg0.add(new Label("wk_sort",sort.getName()));
				
				arg0.add(new AjaxLink("wk_show") {

					@Override
					public void onClick(AjaxRequestTarget arg0) {
						// TODO Auto-generated method stub
						win.setInitialWidth(900);
						win.setContent(new ProductDetailPanel(win.getContentId(), product, service,win));
						win.show(arg0);
					}
				});
			}
		};
		tableContainer.add(listView);
		
		AjaxPagingNavigator navigation=new AjaxPagingNavigator("wk_paging",listView);
		form.add(navigation);
	}

	private void renderTabelContainer(Form form) {
		// TODO Auto-generated method stub
		tableContainer=new WebMarkupContainer("wk_table_container");
		tableContainer.setOutputMarkupId(true);
		form.add(tableContainer);
	}

	private void renderBtns(Form form) {
		// TODO Auto-generated method stub
		form.add(new AjaxButton("wk_add_product") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				win.setInitialWidth(900);
				win.setContent(new ProductDetailPanel(win.getContentId(), service.getRegistProduct(), service,win));
				win.show(target);
			}
		});
		
		form.add(new AjaxButton("wk_del") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				if(checkboxSelectedId.isEmpty()){
					feedbackPanel.error("请选择删除项");
					target.add(feedbackPanel);
				}else{
					warnDialog.showModal(target);
				}
				
			}
		});
	}
	
	private void updateCenterPanel(AjaxRequestTarget arg0){
		JLayouterPanel layouter=(JLayouterPanel) this.getSession().getAttribute("layouter");
		
		layouter.updatePanel(JLayouterPanel.CENTER_PANEL, new ProductGridPanel(JLayouterPanel.CENTER_PANEL), arg0);
	}

}
